<?php  
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-     Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

include_once("includes/db.inc");
include_once("includes/functions.inc");
include ("includes/header.inc");

$action=$_GET['action'];

if ($_POST['code']) $code=return_isbn($_POST['code']); 
if ($_POST['etat_in_ID']) $etat_in_ID=$_POST['etat_in_ID']; 
if ($_POST['location']) $location=$_POST['location']; 
if ($_POST['rpt']) $rpt=$_POST['rpt']; 
if ($_POST['id_tomodif']) $id_tomodif=$_POST['id_tomodif']; 
if ($_POST['livre_titre']) $livre_titre=$_POST['livre_titre']; 
if ($_POST['submit']) $submit=$_POST['submit']; 


if (!($action))
{
   exit;
}

if(!($code))
{
   ?>
   <CENTER>
   <FORM action=pret2.php?action=<?php echo $action; ?> METHOD=POST>
   <h3>
   <?php echo _("CODE DU LIVRE"); ?>
   : <INPUT TYPE="text" NAME="code" SIZE=15></h3>
   </CENTER>
   <?php
}
else 
{
        
        $requete = "SELECT t2.code, t2.nom, t2.prenom,t1.livre_ID, t1.etat_in_ID, t1.etat_out_ID FROM prets as t1 inner join eleves as t2 on t2.id = t1.eleve_ID inner join livres as t3 on t3.id=t1.livre_ID where t3.ISBN='$code' and date_int is NULL"; 
        if(!($resultat = biblio_db_query($requete)))
	{ print "PROBLEME : " .biblio_db_error(); }
	else 
	{  if (mysql_num_rows($resultat)>0)
	   {    
             while ($myrow = biblio_db_fetch_array($resultat)) 
	     {
                   $code_el        =$myrow['code'];
                   $nom_el         =$myrow['nom'];
                   $prenom_el      =$myrow['prenom'];
                   $etat_livre_in  =$myrow['etat_in_ID'];
                   $etat_livre_out =$myrow['etat_out_ID'];
                   $livre_ID_q     =$myrow['livre_ID'];
             }	   
             if($submit)
             {          
                $rpt = $rpt . "<BR>" . $livre_titre . _(" doit &ecirc;tre rang&eacute; dans ") . $location;
                $date_in = date("Y-m-d");
                $requete = "UPDATE prets set date_int = '$date_in', etat_in_ID = $etat_in_ID where id = $id_tomodif";
                if(!($resultat2 = biblio_db_query($requete)))
	        { print "PROBLEME $requete: " .biblio_db_error(); }
	        put_log(3,"PRET IN : $requete",$user);	        
	        if($etat_livre_in != $etat_livre_out)
	        {
	                $requete = "UPDATE livres set etat_ID = $etat_in_ID where id = $livre_ID_q";
                        if(!($resultat2 = biblio_db_query($requete)))
	                { print "PROBLEME $requete: " .biblio_db_error(); }	
	        }
             }
	     $requete = "SELECT t1.date_out, t2.nom, t3.etat, t1.id id_to, t1.etat_out_ID, t5.local, t5.biblio, t5.rangee FROM prets as t1 inner join livres as t2 on t2.id=t1.livre_ID inner join etats as t3 on t3.id = t1.etat_out_ID inner join eleves as t4 on t4.id = t1.eleve_ID inner join locations as t5 on t5.id=t2.location_ID where t4.code='$code_el' and date_int is null  order by date_out"; 

	     if(!($resultat2 = biblio_db_query($requete)))
	     { print "PROBLEME $requete: " .biblio_db_error(); }
	     else 
 	     {  
	        $i=0;	        
	        echo "<CENTER><H3>$prenom_el $nom_el</H3><TABLE><tr><th>Date de sortie</th><th>Titre</th><th>Etat Sortant</th><th>Etat Entrant</th><th>Action</th></tr>";
	        while ($myrow = biblio_db_fetch_array($resultat2)) 
	        {
		       $tab_mois = array('',_('janvier'),_('f&eacute;vrier'),_('mars'),_('avril'),_('mai'),_('juin'),_('juillet'),_('aout'),_('septembre'),_('octobre'),_('novembre'),_('decembre'));
	               $date_sort = $myrow['date_out'];
	               ereg("(....)-(..)-(..)$",$date_sort,$match);
	               $jour  = $match[3];
	               $mois  = sprintf("%d",$match[2]);
	               $annee = $match[1];
	               $date_sort =  "$jour " . $tab_mois[$mois] . " $annee";
	               $etat_out_ID = $myrow['etat_out_ID'];
	               $id_to = $myrow['id_to'];
	               $location = get_location_string($myrow['local'], $myrow['biblio'], $myrow['rangee']);
	               $livre_titre = $myrow['nom'];
	               $i++;
	               if ($i%2 == 1) {$col="#DFE3EF";}
	               else {$col="lightyellow";}
                       echo "<TR bgcolor=$col><TD>".$date_sort."</TD><TD>". $livre_titre ."</TD><TD>".$myrow['etat']."</TD>";
                       ?>
                       <TD><FORM ACTION=pret2.php?action=<?php echo $action; ?>  METHOD=POST><select NAME = "etat_in_ID">
                       <?php
                       $requete = "SELECT * FROM etats order by etat"; 
	               if(!($resultat = biblio_db_query($requete)))
	               { print "PROBLEME : " .biblio_db_error(); }
	               else 
	               {
		                while ($row = biblio_db_fetch_array($resultat)) 
		                {      
		                        $etat_id2 = $row['id'];           
			                $etat_l =  $row['etat'];                                
			                if ($etat_out_ID==$etat_id2) { $str = "selected"; }
			                else { $str="";}
			                print "<option VALUE=\"$etat_id2\" $str>$etat_l";
		                }
	               }                                        
                       ?>
                       </select></TD>
                       <TD>
                       <INPUT TYPE="hidden" NAME="location" VALUE="<?php echo $location; ?>"> 
                       <INPUT TYPE="hidden" NAME="rpt" VALUE="<?php echo $rpt; ?>"> 
                       <INPUT TYPE="hidden" NAME="id_tomodif" VALUE="<?php echo $id_to; ?>"> 
                       <INPUT TYPE="hidden" NAME="livre_titre" VALUE="<?php echo $livre_titre; ?>"> 
                       <INPUT TYPE="hidden" NAME="code" VALUE="<?php echo $code; ?>"> 
                       <INPUT TYPE="submit" name="submit" value="<?php echo("RENTRER"); ?>"></form></TD>
                       <?php
                }
		echo "</table></center>";
              }              
	   }
	   else echo "<CENTER><br><FONT=RED><B>"._("CE LIVRE N'A PAS ETE ENCODE COMME PRETE !!") ."</B></FONT></CENTER>";
	}
        
}
echo "$rpt";
?>
<br><br>
<div>
<span id=left-text>
<A HREF="pret.php">
<?php echo _("retour menu pr&ecirc;ts"); ?>
</A><br>
<A HREF="admin.php?menu=1">
<?php echo _("retour menu administration"); ?>
</A><BR>
<A HREF="index.php">
<?php echo _("retour menu principal"); ?>
</A></span>
</div>
<br>
<br>
<?php
include("includes/footer.inc");
?>
